#!/usr/bin/python

# Copyright (C) 2007-2009 Mihai Preda.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

bytecodesStr = '''RESERVED 0
CONST 0
CALL -1
ADD 2
SUB 2
MUL 2
DIV 2
MOD 2
RND 0
UMIN 1
POWER 2
FACT 1
PERCENT 1
SQRT 1
CBRT 1
EXP 1
LN 1
SIN 1
COS 1
TAN 1
ASIN 1
ACOS 1
ATAN 1
SINH 1
COSH 1
TANH 1
ASINH 1
ACOSH 1
ATANH 1
ABS 1
FLOOR 1
CEIL 1
SIGN 1
MIN 2
MAX 2
GCD 2
COMB 2
PERM 2
LOAD0 0
LOAD1 0
LOAD2 0
LOAD3 0
LOAD4 0
REAL 1
IMAG 1'''

builtins = """rnd sqrt cbrt 
sin cos tan asin acos atan 
sinh cosh tanh asinh acosh atanh 
exp ln 
abs floor ceil sign min max 
gcd comb perm mod real imag"""

template = '''// This file is automatically generated by the build.py script. Do not edit!

/*
 * Copyright (C) 2008-2009 Mihai Preda.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.javia.arity;

class VM {

static final byte
%(bytecodes)s;

static final String[] opcodeName = {%(names)s};

static final byte[] arity = {%(arity)s};

static final byte[] builtins = {%(builtins)s};

}
'''

def genVM():
    bytecodes = [(y[0].lower(), int(y[1])) for y in (x.split() for x in bytecodesStr.split('\n'))]
    str1 = ',\n'.join(['%s = %d' % (name[0].upper(), id) for (name, id) in zip(bytecodes, xrange(256))])
    names = ', '.join(['"%s"' % name[0] for name in bytecodes])
    arity = ', '.join(['%d'%bc[1] for bc in bytecodes])
    builtinsStr = ', '.join([s.upper() for s in builtins.split()])

    fo = open('src/org/javia/arity/VM.java', 'w')
    fo.write(template % dict(bytecodes=str1, names=names, arity=arity, builtins=builtinsStr))
    fo.close()

genVM()

import sys, os
from os import path
import shutil
import glob

def run(cmd):
    #print cmd
    parts = cmd.split()
    err = os.spawnvp(os.P_WAIT, parts[0], parts)
    assert not err, cmd

def mkdir(*dirs):
    for path in dirs:
        try:
            os.makedirs(path)
        except OSError:
            pass

VER='2.1.6'

mkdir('tmp/class', 'rel')

run('rm -rf tmp/class/org')
name = 'rel/arity-'+VER+''
run("javac -sourcepath src -d tmp/class src/org/javia/arity/UnitTest.java")
run("jar cfe %(name)s.jar org.javia.arity.UnitTest -C tmp/class ." % dict(name=name))
#run("javadoc -notimestamp -nodeprecatedlist -notree -noindex -nohelp -noqualifier java.lang -d javadoc -sourcepath src org.javia.arity")
